package com.rq.download_okhttp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.core.content.FileProvider;

import java.io.File;

public class DownloadUtil {
    private static Context ctx;
    private static String saveDir;
    private static String url;
    private static int downIconRes = R.drawable.ic_launcher;

    public static void startDownload(Activity activity, String url, String saveName, DownloadIntentService.DownloadListener listener) {
        activity.startService(DownloadIntentService.getTask(activity, url, saveName, listener));
    }

    public static int getDownIconRes() {
        return downIconRes;
    }

    public static void setDownIconRes(int downIconRes) {
        DownloadUtil.downIconRes = downIconRes;
    }

    public static String getSaveDir() {
        return saveDir;
    }

    public static void setSaveDir(String saveDir) {
        DownloadUtil.saveDir = saveDir;
    }

    public static void setUrl(String baseUrl) {
        DownloadUtil.url = baseUrl;
    }

    public static String getUrl() {
        return DownloadUtil.url;
    }

    public static String getBaseUrl() {
        if (url != null && url.length() > 10) {
            return url.substring(0, 8 + url.substring(8).indexOf("/") + 1);
        }

        return DownloadUtil.url;
    }

    public static void setContext(Context context) {
        DownloadUtil.ctx = context;
    }

    public static Context getCtx() {
        return DownloadUtil.ctx;
    }

    public static void setProxy(Class downLoadClass) {
    }

    //    public static String FileProviderName = BuildConfig.APPLICATION_ID + ".fileProvider";//provider
    //    <provider
    //        android:name="androidx.core.content.FileProvider"
    //        android:authorities="${applicationId}.fileProvider"
    //        android:exported="false"
    //        android:grantUriPermissions="true">
    //        <meta-data
    //            android:name="android.support.FILE_PROVIDER_PATHS"
    //            android:resource="@xml/download_exapmle" />
    //    </provider>
    public static void installApp(Activity context, File file, String FileProviderName) {
        Log.e("DownloadIntentService", "file == null->" + (file == null));
        if (file == null) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //            // 给目标应用一个临时授权
            //            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            //            Uri contentUri = FileProvider.getUriForFile(context, FileProviderName, file);
            //            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");


            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 给目标应用一个临时授权
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, FileProviderName, file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        Log.e("DownloadUtil", "102:" + 1012);
        context.startActivityForResult(intent, 1012);
    }

}
